<template>
  <div class="table-box">
    <div class="table-search">
      <div class="btn-box">
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" plain @click="onExport">导出</el-button>
      </div>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="消息id" align="center" />
          <el-table-column prop="title" label="标题" align="center" />
          <el-table-column prop="content" label="内容" align="center" />
          <el-table-column prop="uid" label="用户id" align="center" />
          <el-table-column prop="lang" label="语言" align="center" />
          <el-table-column prop="app_mark" label="app标识" align="center" />
          <el-table-column
            prop="total_diamond"
            label="总共送的钻石"
            align="center"
          />
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="150"
          >
            <template #default="scope">
              <el-button
                type="danger"
                @click="handleOperate(scope.$index, scope.row, 'delete')"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel } from "@/utils/utils";
import {
  windowListApi,
  windowAddApi,
  windowDelApi,
} from "@/api/slotsgame/message/message";
import DialogForm from "./components/dialogForm.vue";
const loading = ref(false);
const tableData = ref([]);
const btnLoading = ref(false);
const dialogVisible = ref(false);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  title: "",
  content: "",
  uid: "",
  lang: "",
  app_mark: "",
  diamond: "",
};
const dialogForm = reactive({ ...dialogFormData });

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await windowListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 新增
const onAdd = async () => {
  dialogVisible.value = true;
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await windowListApi(data);

  let head = {
    id: "消息id",
    title: "标题",
    content: "内容",
    uid: "用户id",
    lang: "语言",
    app_mark: "app标识",
    total_diamond: "总共送的钻石",
  };
  exportToExcel(res.data.results, head, "Slotsgame-弹窗通知列表");
};

//关闭
const closeDialog = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await windowAddApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

// 操作
const handleOperate = async (index, row, type) => {
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此数据?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await windowDelApi({ id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
